import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { ApiService } from './api.service';
import {People} from '../people';
import {Book} from '../book';
import {resolve} from 'url';
import { Observable } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {el} from '@angular/platform-browser/testing/src/browser_util';
import {Record} from '../record';

@Injectable({
  providedIn: 'root'
})

export class BooksService {

  private api_url ;
  private headers ;

  constructor(private http: Http, private apiService: ApiService) {
    this.api_url = apiService.getUrl() + '/books';
    this.headers = apiService.getHeaders();
  }

  getBooks(): Promise<Book[]> {
    const url = `${this.api_url}`;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book[])
      .catch(this.handleError);
  }

  getBooksByConstraint(book: Book): Promise<Book[]> {
    let url = `${this.api_url}?`;
    console.log(book);
    if (book.bid !== undefined) {
      if (book.bname !== undefined) {
        if (book.number !== undefined) {
          url = `${this.api_url}?bid=${book.bid}&bname=${book.bname}&number=${book.number}`;
        } else {
          url = `${this.api_url}?bid=${book.bid}&bname=${book.bname}`;
        }
      } else {
        if (book.number !== undefined) {
          url = `${this.api_url}?bid=${book.bid}&number=${book.number}`;
        } else {
          url = `${this.api_url}?bid=${book.bid}`;
          console.log(url);
        }
      }
    } else {
      if (book.bname !== undefined) {
        if (book.number !== undefined) {
          url = `${this.api_url}?bname=${book.bname}&number=${book.number}`;
        } else {
          url = `${this.api_url}?bname=${book.bname}`;
        }
      } else {
        if (book.number !== undefined) {
          url = `${this.api_url}?number=${book.number}`;
        } else {
          url = `${this.api_url}`;
          console.log(url);
        }
      }
    }
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book[])
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error);
    return Promise.reject(error.message || error);
  }

  getBookByAny(bid: string, bname: string): Promise<Book> {
    const url = `${this.api_url}/${bid}`;
    return this.http.get(url, {headers: this.headers})
        .toPromise()
        .then(res => res.json() as Book)
        .catch(this.handleError);
  }

  getBookBybid(bid: string): Promise<Book> {
    const url = `${this.api_url}?bid=${bid}`;
    return this.http.get(url, {headers: this.headers})
        .toPromise()
        .then(res => res.json() as Book)
        .catch(this.handleError);
  }

  updateNumber(book: Book): Promise<Book> {
    const url = `${this.api_url}/${book.id}`;
    return this.http
      .put(url, JSON.stringify(book), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book)
      .catch(this.handleError);
  }
  createBook(book: Book): Promise<Book> {
    const url = `${this.api_url}`;
    return this.http
      .post(url, JSON.stringify(book), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book)
      .catch(this.handleError);
  }

  // 根据所有属性新增某本书
  createBookByAll(bid: string, bname: string, description: string, number: number): Promise<Book> {
    const book = {
      bid: bid,
      bname: bname,
      description: description,
      number: number
    }
    const url = `${this.api_url}`;
    return this.http
      .post(url, JSON.stringify(book), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book)
      .catch(this.handleError);
  }

  // 删除某本Book
  deleteBook(book: Book): Promise<Book> {
    const url = `${this.api_url}/${book.id}`;
    return this.http.delete(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book)
      .catch(this.handleError);
  }

  // 按id删除某本Book
  deleteBookById(id: number): Promise<Book> {
    const url = `${this.api_url}/${id}`;
    return this.http.delete(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Book)
      .catch(this.handleError);
  }

}
